<?php

if (!class_exists("stthemehook")) {

    class stthemehook {

        public $option = '';

        public function __construct($config) {
            $this->config = $config;
            $this->config ['themeDir'] = TEMPLATEPATH . '/admin-functions/';
            $this->config['includePath'] = TEMPLATEPATH . '/stInclude/';
            $this->option = get_option($this->config['theme_name'] . '_' . $this->config['option_key'] . '_settings');
            $this->hookManage();
            $this->hookCustom();
        }

        public function loadmodel($modelName) {
            $className = $modelName . "_model";
            $fileName = $this->config ['includePath'] . "models/{$modelName}.php";
            require_once $fileName;
            $obj = new $className($this->config);
            return $obj;
        }

        public function hookManage() {
            $method = array($this, 'version');
            add_action('wp_head', $method);
            add_action('admin_head', $method);

            $method = array($this, 'title');
            add_filter('wp_title', $method, 10, 2);
            if(!is_admin ()){
            $method = array($this, 'registerStyle');
            add_action('wp_print_styles', $method);
            }

            $method = array($this, 'afterSetup');
            add_action('after_setup_theme', $method);

            $method = array($this, 'languageStart');
            add_filter('load_textdomain_mofile', $method, 100, 2);

            $method = array($this, 'includeExtraPlugin');
            add_action($this->config['plugin_name'] . '_extra_contruction', $method);

            $method = array($this, 'tmceInit');
            add_action('admin_enqueue_scripts', $method, 25);

            $method = array($this, 'widgetsInit');
            add_action('widgets_init', $method);
            $method = array($this, 'registerStWidgets');
            add_action( 'widgets_init', $method);
        }

        public function hookCustom() {
            
        }

        //Initialize hook function

        public function version() {
            $themeexpert_version = "1.0";
            update_option('themeexpert_version', $themeexpert_version);

            $theme_data = get_theme_data(TEMPLATEPATH . '/style.css');
            $theme_version = $theme_data['Version'];

            echo '<meta name="generator" content="' . $this->config['theme_name'] . ' ' . $theme_version . '" />' . "\n";
            echo '<meta name="generator" content="themeExpert Framework Version ' . $themeexpert_version . '" />' . "\n";
        }

        public function title() {
            // Don't affect wp_title() calls in feeds.
            if (is_feed())
                return $title;

            // The $paged global variable contains the page number of a listing of posts.
            // The $page global variable contains the page number of a single post that is paged.
            // We'll display whichever one applies, if we're not looking at the first page.
            global $paged, $page;

            if (is_search()) {
                // If we're a search, let's start over:
                $title = sprintf(__('Search results for %s', 'Knerten'), '"' . get_search_query() . '"');
                // Add a page number if we're on page 2 or more:
                if ($paged >= 2)
                    $title .= " $separator " . sprintf(__('Page %s', 'Knerten'), $paged);
                // Add the site name to the end:
                $title .= " $separator " . get_bloginfo('name', 'display');
                // We're done. Let's send the new title back to wp_title():
                return $title;
            }

            // Otherwise, let's start by adding the site name to the end:
            $title .= get_bloginfo('name', 'display');

            // If we have a site description and we're on the home/front page, add the description:
            $site_description = get_bloginfo('description', 'display');
            if ($site_description && ( is_home() || is_front_page() ))
                $title .= " $separator " . $site_description;

            // Add a page number if necessary:
            if ($paged >= 2 || $page >= 2)
                $title .= " $separator " . sprintf(__('Page %s', 'Knerten'), max($paged, $page));

            // Return the new title to wp_title():
            return $title;
        }

        public function registerStyle() {
            wp_enqueue_style('stThemeStyle', get_bloginfo('template_directory') . '/style.css');
        }

        public function afterSetup() {
            // This theme styles the visual editor with editor-style.css to match the theme style.
            add_editor_style();

            // This theme uses post thumbnails
            add_theme_support('post-thumbnails');

            // Make theme available for translation
            // Translations can be filed in the /languages/ directory
            load_theme_textdomain($this->config['theme_name'], TEMPLATEPATH . '/languages');

            $locale = get_locale();
            $locale_file = TEMPLATEPATH . "/languages/$locale.php";
            if (is_readable($locale_file))
                require_once( $locale_file );

            register_nav_menus(array(
                'primary' => __('Primary Navigation', $this->config['theme_name']),
            ));
        }

        public function includeExtraPlugin() {
            
        }

        public function languageStart($mofile, $domain) {
            global $locale;

            //$fbcontent_settings = get_option('fbcontent_settings');
            $language = $this->option['st_language'];

            if ($domain == $this->config['plugin_name']) {
                $mofile = str_replace($locale, $language, $mofile);
                return $mofile;
            }
            return $mofile;
        }

        function tmceInit() {
            global $wp_scripts;
            $queue = $wp_scripts->queue;
            if (is_array($queue) && in_array('editor', $queue, true)) {
                wp_enqueue_script('fixAutotop', WP_CONTENT_URL . '/themes/' . $this->config['theme_name'] . '/admin-functions/scripts/sttheme.js', array('editor'));
                echo '<script type="text/javascript">addLoadEvent(function(){sttheme.fixAutoTop();});</script>';
            }
        }

        public function registerNavMenu() {
            register_nav_menus(array(
                'mainMenu' => __('Main Navigation', $this->config['theme_name']),
                'sideMenu' => __('Side Navigation', $this->config['theme_name']),
            ));
        }

        function widgetsInit() {}

        function registerStWidgets() {
            $widgets = scandir($this->config ['themeDir'] . 'widgets');
            for ($i = 2; $i < count($widgets); $i++) {
				if($widgets[$i] == '.svn' ) continue;
                $class_name = substr($widgets[$i], 0, count($widgets[$i]) - 5);
                $fileName = $this->config ['themeDir'] . "/widgets/{$widgets[$i]}";
                require_once $fileName;
                register_widget($class_name);
            }
        }

    }

}
?>